<?php


/**
* CakePHP Routines Plugin
* http://cake-routines-plugin.googlecode.com
*
* @package routines
* @subpackage routines.models.behaviors
* @author Ivan Chistyakov <zetruger@gmail.com>
* @version $Id: neighborable.php 153 2010-10-20 07:14:31Z zetruger@gmail.com $
* @link $HeadURL: http://cake-routines-plugin.googlecode.com/svn/trunk/models/behaviors/neighborable.php $
* @license MIT License (http://opensource.org/licenses/mit-license.php)
*/


class NeighborableBehavior extends ModelBehavior {

var $_defaults = array(
	'neighborhood' => array(),
	'order' => NULL,
	'findCallbacks' => TRUE,
	'neighborhoodConditions' => array()
);

var $settings = array();

function setup(&$Model, $settings = array()) {
	$this->settings[$Model->alias] = $settings;
}

function _getSettings(&$Model, $settings = array()) {
	$settings = Set::merge($this->settings[$Model->alias], $settings);
	if (isset($settings['noUseModelVars']) && $settings['noUseModelVars']) {
		return Set::merge($this->_defaults, $settings);
	} else {
		return Set::merge($this->_defaults, array_intersect_key(get_object_vars($Model), $this->_defaults), $settings);
	}
}

function getNeighborhoodConditions(&$Model, $id) {
	extract($this->_getSettings($Model), EXTR_SKIP);
	if (empty($neighborhood)) {
		return array();
	} elseif ($neighborhood === TRUE) {
		$neighborhood = array();
		if ($Model->Behaviors->attached('Associable')) {
			//
		} elseif ($Model->Behaviors->attached('Tree')) {
			//
		}
	}
	$data = $Model->find('first', array('conditions' => array($Model->escapeField($Model->primaryKey) => $id), 'fields' => array_map(array($Model, 'escapeField'), $neighborhood), 'recursive' => -1, 'callbacks' => $findCallbacks));
	if (isset($data[$Model->alias]) && is_array($data[$Model->alias])) {
		return array_combine(array_map(array($Model, 'escapeField'), array_keys($data[$Model->alias])), $data[$Model->alias]);
	}
	return FALSE;
}

function _conditions(&$Model, $id) {
	return $this->getNeighborhoodConditions($Model, $id);
}

}


?>